<template>
  <div style="width:100%;" class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="房主姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t('handle.search') }}</el-button>
        <el-button  type="primary" @click="addOrUpdateHandle()">{{ $t('handle.add') }}</el-button>
        <el-button  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">{{ $t('handle.batchDelete') }}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      ref="table"
      height="500"
      border
      style="width: 100%;"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        >
      </el-table-column>
      <!-- <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="主键id">
      </el-table-column> -->
      <!-- <el-table-column
        prop="typeText"
        header-align="center"
        align="center"
        label="类别">
        <template slot-scope="scope">
          {{fwlbType(scope.row.type)}}
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        prop="nature"
        header-align="center"
        align="center"
        label="性质">
        <template slot-scope="scope">
          {{fwxzType(scope.row.nature)}}
        </template>
      </el-table-column> -->
       <el-table-column
        prop="houseAdreess.addressName"
        header-align="center"
        :show-overflow-tooltip="true"
        align="center"
        label="标准地址">
      </el-table-column>
      <el-table-column
        prop="ownerEntity.name"
        header-align="center"
        align="center"
        label="房主">
      </el-table-column>
      <el-table-column
        prop="purpose"
        header-align="center"
        :show-overflow-tooltip="true"
        align="center"
        label="用途">
        <template slot-scope="scope">
          {{fwytType(scope.row.purpose)}}
        </template>
      </el-table-column>
      <el-table-column
        prop="number"
        header-align="center"
        align="center"
        label="间数">
      </el-table-column>
      <!-- <el-table-column
        prop="area"
        header-align="center"
        align="center"
        label="面积">
      </el-table-column> -->
      <el-table-column
        prop="propertynumber"
        header-align="center"
        align="center"
        label="房产证号">
      </el-table-column>
      <el-table-column
        prop="dgActualUnitEntity.name"
        header-align="center"
        align="center"
         :show-overflow-tooltip="true"
        label="实有单位">
      </el-table-column>

      <!-- <el-table-column
        prop="custodianId"
        header-align="center"
        align="center"
        label="托管人">
      </el-table-column>
      <el-table-column
        prop="registrantName"
        header-align="center"
        align="center"
        label="登记人">
      </el-table-column>
      <el-table-column
        prop="registrantTime"
        header-align="center"
        align="center"
        label="登记时间">
      </el-table-column>
      <el-table-column
        prop="logoffTime"
        header-align="center"
        align="center"
        label="注销时间">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="更新时间">
      </el-table-column> -->
      <el-table-column
        header-align="center"
        align="center"
        :label="$t('handle.handle')">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id,true)">{{ $t('handle.detail') }}</el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t('handle.update') }}</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('handle.delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './add-or-update.vue'
  import { getDictList,getPopulationMessage,getUnitMessage } from '@/utils'
  export default {
    data () {
      return {
        communityId:'',
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        fwytOptions: [],
        fwlbOptions: [],
        fwxzOptions: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
    //  this.getDataList()
    },
    created () {
      this.initData()

    },
    computed: {
      // regionId() {
      //   return this.$store.state.address.regionId;
      // },
      regionId() {
        return this.$store.state.address.regionId;
      },
      fwytType () {
        return function (type) {
          return this.fwytOptions[type]
        }
      }
      // fwlbType () {
      //   return function (type) {
      //     return this.fwlbOptions[type]
      //   }
      // },
      // fwxzType() {
      //   return function (type) {
      //     return this.fwxzOptions[type]
      //   }
      // },
    },
    methods: {
      adjust(communityId){
        this.communityId = communityId;
        setTimeout(()=>{
          this.$refs['table'].doLayout()
        },100)
      },
      initData() {
        getDictList('house_usage').then(({data}) => {
          for(let i=0;i < data.dictList.length;i++){
            this.fwytOptions[data.dictList[i].value] = data.dictList[i].label
          }
        })
        getDictList('house_type').then(({data}) => {
          for(let i=0;i < data.dictList.length;i++){
            this.fwlbOptions[data.dictList[i].value] = data.dictList[i].label
          }
        })
        getDictList('house_property').then(({data}) => {
          for(let i=0;i < data.dictList.length;i++){
            this.fwxzOptions[data.dictList[i].value] = data.dictList[i].label
          }
          this.getDataList()
        })
      },
      // 获取数据列表
      getDataList () {
        let _this = this
        this.dataListLoading = true
        this.$http({
          // url: this.$http.adornUrl('/dgactualhouse/gethouse'),
          url: '/dg-standard-info/dgactualhouse/list',
          method: 'post',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.dataForm.key === null?'':this.dataForm.key,
            'regionId':this.regionId
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            // data.page.list.map(function (item) {
            //   //item.portraitId='http://192.168.0.14/group1/M00/00/00/wKgADly9np2AfHuUAACCsRwKApo349.jpg'
            //   getPopulationMessage(item.ownerId).then(({data}) => {
            //     if(data.dgActualPopulation){
            //       item.ownerId = data.dgActualPopulation.name
            //     }
            //   })
            //    getUnitMessage(item.unitId).then(({data}) => {
            //     if(data.dgActualUnit){
            //       item.unitId = data.dgActualUnit.name
            //     }
            //   })
            //   item.nature = _this.fwxzOptions[item.nature]
            //   item.purpose = _this.fwytOptions[item.purpose]

            //   return item
            // })
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.msg);
          }
          this.dataListLoading = false
        },
          error => {
            if (error.response.status === 403) {
              this.$message.error("你没有权限");
            } else {
              this.$message.error("系统错误，请联系管理员");
            }
          })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id,detail) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, detail,this.communityId)
          this.$refs.addOrUpdate.initHouse(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定进行删除操作嘛?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            // url: this.$http.adornUrl('/dgactualhouse/delete'),
            url: `/dg-ic/ic/room/delete`,
            method: 'delete',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
<style >
  #pane-three{
    height: 100%;
    overflow: auto;
  }
</style>

